package com.foreverwin.mes.warehouse.controller;

import com.foreverwin.mes.core.base.AjaxResult;
import com.foreverwin.mes.core.utils.CommonMethods;
import com.foreverwin.mes.warehouse.dto.YieldStorageDto;
import com.foreverwin.mes.warehouse.service.YieldStorageService;
import com.foreverwin.mes.warehouse.vo.YieldStorageVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Ervin Chen
 * @date 2020/2/14 16:22
 */
@RestController
@RequestMapping("/yieldStorages")
public class YieldStorageController {
    private final Logger logger = LoggerFactory.getLogger(YieldStorageController.class);

    @Autowired
    private YieldStorageService yieldStorageService;

    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("")
    public AjaxResult getInventoryList(YieldStorageDto yieldStorageDto) {
        List<YieldStorageVo> result;
        try {
            result = yieldStorageService.selectInventoryList(yieldStorageDto);
        } catch (Exception e) {
            logger.error("getInventoryList -=- {}", e.toString());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(result);
    }

    /**
     * 保存和修改公用
     *
     * @param yieldStorageDtos 传递的实体
     * @return null 失败  实体成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/_save")
    public AjaxResult yieldStorageSave(@RequestBody List<YieldStorageDto> yieldStorageDtos) {
        String site = CommonMethods.getSite();
        String user = CommonMethods.getUser();
        int count;
        try {
            count = yieldStorageService.insertOrUpdate(yieldStorageDtos) ? 1 : 0;
        } catch (Exception e) {
            logger.error("yieldConfirmSave -=- {}", e.toString());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(count == 0 ? null : yieldStorageDtos);
    }
}
